package robotinterface.list;

import java.awt.event.MouseEvent;
import java.io.*;
import java.util.concurrent.*;
import javax.swing.*;
import robotinterface.client.RobotClient;
import robotinterface.list.actions.*;
import robotinterface.list.entry.*;
import robotinterface.list.listeners.*;


/**
 *
 * @author Brian Bailey
 */
public class RobotList<T> extends JList {

    private static final boolean VERBOSE = true;
    
    private ExecutorService executor = Executors.newFixedThreadPool(2);    
    
    public boolean isRunning = false;
    public boolean isForward = true;
    
    private ListTransferListener listTransferListener;
    private ListContextMenu contextMenu;
    private EditListAction editAction;
    private RobotListModel listModel;
    private ListAction listAction;


    public RobotList() {
        super();
    }

    public RobotList(RobotListModel listModel) {
        
        /* List Cell Edit Action*/
        this.editAction = new EditListAction();
        this.listAction = new ListAction(this, editAction);

        /* Context Menu */
        this.contextMenu = new ListContextMenu(this);
        this.addMouseListener(contextMenu);

        this.listModel = listModel;
        this.setModel(listModel);

    }
   
    public void setIndex(int index) {
        listModel.setIndex(index);
    }

    public int getIndex() {
        return listModel.getIndex();
    }

    public Entry getElementAt(int index) {
        return (Entry) listModel.getElementAt(index);
    }

    @Override
    public String getToolTipText(MouseEvent event) {
        int index = locationToIndex(event.getPoint());
        return getElementAt(index).getToolTip();
    }

    public void setListDragListener(ListTransferListener listTransferListener) {
        this.listTransferListener = listTransferListener;
    }

    public void readObject(String varName) {
        try { listModel.readObject(varName); } 
        catch (ClassNotFoundException | IOException ex) { }
    }

    public void writeObject(String varName) {
        try { listModel.writeObject(varName); } 
        catch (IOException ex) { }
    }
    
    public void stepForward() {        
        Entry aEntry = listModel.stepForward();
        setSelectedValue(aEntry, true);
        
        executeTask(aEntry);
        setSelectedIndex(listModel.getIndex());
        
        if (VERBOSE) { System.err.println("RobotList.stepForward() Entry: " + aEntry); }
    }

    public void stepBackward() {
        Entry aEntry = listModel.stepBackward();
        setSelectedValue(aEntry, true);

        executeTask(aEntry);
        setSelectedIndex(listModel.getIndex());
        
        if (VERBOSE) { System.err.println("RobotList.stepBackward() Entry: " + aEntry); }
    }        
    
    public void stop() {
        isRunning = false;
        listModel.cancelled();        
        
        setSelectedIndex( listModel.getIndex() );        
    }
   
    public void start() {
        isRunning = true;
         if (isForward) { stepForward(); }
         else { stepBackward(); }
    }

    public void executeTask(final Entry entry) {
        if (isRunning) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    RobotClient.sendMessage(entry.getTitle());
                }
            });
        }
    }

    public void gotMessage(String buffer) {
        if (VERBOSE) { System.err.println("RobotList.gotMessage(): " + buffer); }
        if (isRunning) {
            if (isForward) { stepForward(); }
            else { stepBackward(); }
        }
    }
    
}
